package com.example.chang.law_doc.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

import com.example.chang.law_doc.R;
import com.example.chang.law_doc.utils.ImmersiveStatusbar;
import com.example.chang.law_doc.utils.ShareUtils;
import com.example.chang.law_doc.utils.StaticClass;

/**
 * Created by chang on 2018/4/15.
 * 项目描述：
 */

public class SplashActivity extends AppCompatActivity{
    private TextView tv_time;
    MyCountTimer timer;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case StaticClass.HANDLER_SPLASH:
                    //判断程序是否是第一次运行
                    if (isFirst()) {
                        startActivity(new Intent(SplashActivity.this, LoginActivity.class));
                    } else {
                        startActivity(new Intent(SplashActivity.this, LoginActivity.class));
                    }
                    finish();
                    break;
            }
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        ImmersiveStatusbar.getInstance().Immersive(getWindow(),getSupportActionBar());
        initView();
    }

    private void initView() {
        handler.sendEmptyMessageDelayed(StaticClass.HANDLER_SPLASH, 3000);
        tv_time= (TextView) findViewById(R.id.tv_time);
        timer =new MyCountTimer(4000,1000);
        timer.start();
    }
    //判断程序是否第一次运行
    private boolean isFirst() {
        boolean isFirst = ShareUtils.getBoolean(this,StaticClass.SHARE_IS_FIRST,true);
        if(isFirst){
            ShareUtils.putBoolean(this,StaticClass.SHARE_IS_FIRST,false);
            //是第一次运行
            return true;
        }else {
            return false;
        }

    }
    class MyCountTimer extends CountDownTimer{
        public MyCountTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long l) {
            tv_time.setText((l/1000)+"");
        }

        @Override
        public void onFinish() {
        }
    }
}
